package com.esint;

import redis.clients.jedis.Jedis;

/**
 * @description: redis Set类型测试类
 * @author: 谷明
 * @createDate: 2021/8/12 1:10
 */
public class TestSet {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.43.166", 6379);
        jedis.flushDB();

        System.out.println("======================添加一个集合，并添加元素======================");
        System.out.println("初始化集合：" + jedis.sadd("eleSet", "e1", "e2", "e11", "e12", "e3", "e5", "e8"));
        System.out.println("向已有集合添加一个元素：" + jedis.sadd("eleSet", "e6"));
        System.out.println("向已有集合添加一个重复元素：" + jedis.sadd("eleSet", "e6"));
        System.out.println("查看集合所有元素：" + jedis.smembers("eleSet"));
        System.out.println("删除指定元素e11：" + jedis.srem("eleSet", "e11"));
        System.out.println("删除后查看集合所有元素：" + jedis.smembers("eleSet"));
        System.out.println("删除指定多个元素：" + jedis.srem("eleSet", "e6", "e5"));
        System.out.println("删除后查看集合所有元素：" + jedis.smembers("eleSet"));
        System.out.println("随机删除一个元素：" + jedis.spop("eleSet"));
        System.out.println("删除后查看集合所有元素：" + jedis.smembers("eleSet"));
        System.out.println("随机删除一个元素：" + jedis.spop("eleSet"));
        System.out.println("删除后查看集合所有元素：" + jedis.smembers("eleSet"));
        System.out.println("查看集合元素个数：" + jedis.scard("eleSet"));
        System.out.println("e1是否在eleSet中：" + jedis.sismember("eleSet", "e1"));
        System.out.println("e8是否在eleSet中：" + jedis.sismember("eleSet", "e8"));
        System.out.println("e12是否在eleSet中：" + jedis.sismember("eleSet", "e12"));
        System.out.println("============================================");

        System.out.println("======================添加另外两个集合，并添加元素======================");
        System.out.println("初始化集合eleSet1：" + jedis.sadd("eleSet1", "e1", "e2", "e11", "e12", "e3", "e5", "e88", "e99", "e8"));
        System.out.println("初始化集合eleSet2：" + jedis.sadd("eleSet2", "e2", "e11", "e3", "e5", "e8"));
        System.out.println("将eleSet1中删除e99并存入新集合eleSet3中：" + jedis.smove("eleSet1","eleSet3", "e99"));
        System.out.println("将eleSet1中删除e88并存入新集合eleSet3中：" + jedis.smove("eleSet1","eleSet3", "e88"));
        System.out.println("集合eleSet1中所有元素：" + jedis.smembers("eleSet1"));
        System.out.println("集合eleSet3中所有元素：" + jedis.smembers("eleSet3"));

        System.out.println("======================集合运算======================");
        System.out.println("集合eleSet1中所有元素：" + jedis.smembers("eleSet1"));
        System.out.println("集合eleSet2中所有元素：" + jedis.smembers("eleSet2"));
        System.out.println("集合eleSet1和eleSet2的交集：" + jedis.sinter("eleSet1","eleSet2"));
        System.out.println("集合eleSet1和eleSet2的并集：" + jedis.sunion("eleSet1","eleSet2"));
        System.out.println("集合eleSet1和eleSet2的差集：" + jedis.sdiff("eleSet1","eleSet2"));
        System.out.println("求集合eleSet1和eleSet2的交集并存入新的集合eleSet4中：" + jedis.sinterstore("eleSet4","eleSet1","eleSet2"));
        System.out.println("求集合eleSet1和eleSet2的并集并存入新的集合eleSet5中：" + jedis.sunionstore("eleSet5","eleSet1","eleSet2"));
        System.out.println("求集合eleSet1和eleSet2的差集并存入新的集合eleSet6中：" + jedis.sdiffstore("eleSet6","eleSet1","eleSet2"));
        System.out.println("集合eleSet4中所有元素：" + jedis.smembers("eleSet4"));
        System.out.println("集合eleSet5中所有元素：" + jedis.smembers("eleSet5"));
        System.out.println("集合eleSet6中所有元素：" + jedis.smembers("eleSet6"));
    }
}
